package cn.aylog;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class Demo {

    @Test
    public void run1() {
        // 获取操作主体
        Subject subject = SecurityUtils.getSubject();
        // 设置校验token
        AuthenticationToken token = new UsernamePasswordToken("admin", "123");
        try{
            subject.login(token);
            System.out.println("登录成功");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
        } catch (UnknownAccountException e) {
            System.out.println("用户名不存在");
        } catch (Exception e) {
            System.out.println("登录失败");
        }
        // 断言
        Assert.assertEquals(true, subject.isAuthenticated());
        // 登出
        subject.logout();
    }

    @Test
    public void run2() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "111");
        map.put("name", "aylog");
        System.out.println(map);
    }
}
